package com.algorithms.JKTime.array.LongestSubstringWithoutRepeatingCharacters;// 3. Longest Substring Without Repeating Characters
// https://leetcode.com/problems/longest-substring-without-repeating-characters/description/


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

// 时间复杂度: O(len(s))
// 空间复杂度: O(len(charset))
public class Solution7 {

    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int end = 0, start = 0; end < n; end++) {
            char alpha = s.charAt(end);
            if (map.containsKey(alpha)) {
                start = Math.max(map.get(alpha), start);
            }
            ans = Math.max(ans, end - start + 1);
            map.put(s.charAt(end), end + 1);
        }
        return ans;
    }

    public static void main(String[] args) {

        System.out.println((new Solution7()).lengthOfLongestSubstring( "abcabcbb" ));
        System.out.println((new Solution7()).lengthOfLongestSubstring( "bbbbb" ));
        System.out.println((new Solution7()).lengthOfLongestSubstring( "pwwkew" ));
        System.out.println((new Solution7()).lengthOfLongestSubstring( "" ));
    }
}
